using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	
	private Vector3 spawnPoint;
	private float moveSpeed = 15;
	private int moveDirX;
	private int moveDirY;
	private Vector3 movement;
	private OTAnimatingSprite sprite;
	private GUIText guiTextAcertos;
	private int acertosNumber = 0;
	
	// Use this for initialization
	void Start () {
		Vars.alive = true;
		Vars.falling = true;		
		spawnPoint = transform.position; // player will respawn at initial starting point
		
		sprite = GetComponent<OTAnimatingSprite>();
	    // Set this sprite's collision delegate 
	    // HINT : We could use sprite.InitCallBacks(this) as well.
	    // but because delegates are the C# way we will use this technique
	    sprite.onCollision = OnCollision;
		sprite.onExit = OnExit;
		
		guiTextAcertos = GameObject.Find("gui_text_acertos_value").guiText;
	}
	
	// Update is called once per frame
	void Update () {			
		
		moveDirX = 0;
		moveDirY = 0;
		
		// move left
		if(Vars.isLeft) 
		{
			
			moveDirX = -1;
			Vars.facingDir = 1;
		}
		
		// move right
		if(Vars.isRight) 
		{
			moveDirX = 1;
			Vars.facingDir = 2;
		}		
		
		UpdateMovement();
	}
	
	void UpdateMovement() 
	{
		// player is not falling so move normally
		if(!Vars.falling) 
		{
			movement = new Vector3(moveDirX, moveDirY,0f);
			movement *= Time.deltaTime*moveSpeed;
			transform.Translate(movement.x,movement.y, 0f);
		}
		
		// player is falling so apply gravity
		else 
		{
			movement = new Vector3(0f,-1f,0f);
			movement *= Time.deltaTime*moveSpeed;
			transform.Translate(0f,movement.y, 0f);
		}
	}	
	
	
	/**
	 * Collid with only orthello OTObject
	 */
	// OnCollision delegate is called when star collides with another 'collidable' object
    // HINT - OnEnter and OnCollision delegates are called exactly at the same time, the only
    // difference is their naming convention
    /// <exclude />
	public void OnCollision(OTObject owner)
	{
			    		
		if (owner.collisionObject.tag.IndexOf("ground_tag") == 0)
		{
			//print("Ground collision");
			Vars.falling = false;			
		}
		
		if (owner.collisionObject.tag.IndexOf("missile_tag") == 0)
		{
			acertosNumber++;
			guiTextAcertos.text = acertosNumber.ToString();
			transform.position = spawnPoint;
			print("missile collision");			
		}		

		if (owner.collisionObject.tag.IndexOf("dead_zone_tag") == 0)
		{
			transform.position = spawnPoint;			
			print("dead zone");			
		}		
		
		//if (owner.collisionObject.name.IndexOf("top") == 0)
        //{
		//}
		
	}
	
	// OnStay delegate is called when star enters (overlaps) another 'collidable' object
    // !IMPORTANT - This sprite's collidable setting has to be true otherwide
    // collision delegates will not be called
    /// <exclude />	
	public void OnStay(OTObject owner)
	{
		
		//Vars.falling = false;
		//print("onStay!");
	}
	
	// OnExit delegate is called when star no longer overlaps another 'collidable' object
    /// <exclude />
	public void OnExit(OTObject owner)
	{
	
		if (owner.collisionObject.tag.IndexOf("ground_tag") == 0)
		{
			Vars.falling = true;
		}				
		
	}
}
